home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Panorama / Panorama - Disk 30B (1988-06-08)(Pacific North-West Amigas Club)[WB].zip / Panorama - Disk 30B (1988-06-08)(Pacific North-West Amigas Club)[WB].adf / DNet1.20 / client / loadav.c < prev    next >
C/C++ Source or Header  |  1988-03-22  |  5KB  |  243 lines

  1.  
  2. /*
  3.  *  LOADAV.C
  4.  *
  5.  *  DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  LOADAV  [frequency]     Connect to remote UNIX system and display
  8.  *                load average.  Default is every 5 minutes.
  9.  *
  10.  *  frequency in seconds, default is every 60 seconds
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include "/include/typedefs.h"
  16. #include "/dnet/channel.h"
  17.  
  18. #include "/server/servers.h"
  19. #define BASELOAD    2
  20. #define LOADINCR    2
  21.  
  22. short MaxLoad = BASELOAD;
  23. ubyte Title[64] = { "LoadAv" };
  24.  
  25. typedef struct timerequest IOT;
  26.  
  27. NW Nw = {
  28.     640-150, 0, 150, 50, -1, -1,
  29.     NEWSIZE|CLOSEWINDOW,
  30.     WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|NOCAREREFRESH,
  31.     NULL, NULL, Title, NULL, NULL,
  32.     32, 18, -1, -1, WBENCHSCREEN
  33. };
  34.  
  35. WIN *Win;
  36. RP  *Rp;
  37.  
  38. int Enable_Abort;
  39. char Buf[512];
  40. char Cc;
  41.  
  42. extern void *OpenWindow();
  43. extern void *GetMsg();
  44. extern void *CreatePort();
  45.  
  46. long IntuitionBase;
  47. long GfxBase;
  48.  
  49. main(ac,av)
  50. char *av[];
  51. {
  52.     long chan = NULL;
  53.     short numsecs = 60;
  54.     long n;
  55.     long imask, tmask, dmask, mask;
  56.     char notdone = 1;
  57.     char *host = NULL;
  58.     PORT *TimPort = CreatePort(NULL, 0);
  59.     IOT Iot;
  60.  
  61.     {
  62.     register short i;
  63.     for (i = 1; i < ac; ++i) {
  64.         if (strncmp(av[i], "-N", 2) == 0) {
  65.         host = av[i]+2;
  66.         continue;
  67.         }
  68.         numsecs = atoi(av[i]);
  69.     }
  70.     }
  71.  
  72.  
  73.     Iot.tr_node.io_Device = NULL;
  74.  
  75.     if (OpenDevice("timer.device", UNIT_VBLANK, &Iot, 0))
  76.     goto fail;
  77.     Iot.tr_node.io_Command = TR_ADDREQUEST;
  78.     Iot.tr_node.io_Message.mn_ReplyPort = TimPort;
  79.     Iot.tr_time.tv_micro = 1;
  80.     Iot.tr_time.tv_secs  = 0;
  81.     SendIO(&Iot);
  82.  
  83.     Enable_Abort = 0;
  84.     IntuitionBase = OpenLibrary("intuition.library", 0);
  85.     GfxBase = OpenLibrary("graphics.library", 0);
  86.  
  87.     chan = DOpen(host, PORT_LOADAV, 25, 25);
  88.     if (chan == NULL) {
  89.     puts("no connect");
  90.     goto fail;
  91.     }
  92.     Win = OpenWindow(&Nw);
  93.     if (Win == NULL) {
  94.     puts("Unable to open window");
  95.     goto fail;
  96.     }
  97.     Rp = Win->RPort;
  98.     imask   = 1 << Win->UserPort->mp_SigBit;
  99.     dmask   = 1 << ((PORT *)chan)->mp_SigBit;
  100.     tmask   = 1 << TimPort->mp_SigBit;
  101.  
  102.     clearwindow();
  103.     while (notdone) {
  104.     mask = Wait(imask|dmask|tmask|SIGBREAKF_CTRL_C);
  105.     if (mask & SIGBREAKF_CTRL_C)
  106.         notdone = 0;
  107.     if (mask & imask) {
  108.         IMESS *im;
  109.         while (im = GetMsg(Win->UserPort)) {
  110.         switch(im->Class) {
  111.         case NEWSIZE:
  112.             clearwindow();
  113.             break;
  114.         case CLOSEWINDOW:
  115.             notdone = 0;
  116.             break;
  117.         }
  118.         ReplyMsg(im);
  119.         }
  120.     }
  121.     if (mask & dmask) {
  122.         char dummy;
  123.         if ((dummy = DNRead(chan, &dummy, 1)) != 0)
  124.         notdone = 0;
  125.     }
  126.     while (mask & tmask) {      /*  while just so we can break */
  127.         char len = 0;
  128.         char buf[64];
  129.         long onei,onef,fivei,fivef,teni,tenf;
  130.  
  131.         if (GetMsg(TimPort)) {
  132.         Iot.tr_time.tv_micro = 0;
  133.         Iot.tr_time.tv_secs  = numsecs;
  134.         SendIO(&Iot);
  135.         if (DWrite(chan, &len, 1) == 1 && DRead(chan, &len, 1) == 1) {
  136.             if (DRead(chan, buf, len) == len) {
  137.             sscanf(buf, "%ld.%ld %ld.%ld %ld.%ld",
  138.                 &onei,&onef,&fivei,&fivef,&teni,&tenf
  139.             );
  140.             updatewindow(onei,onef,fivei,fivef,teni,tenf);
  141.             break;
  142.             }
  143.         }
  144.         notdone = 0;
  145.         }
  146.         break;
  147.     }
  148.     }
  149.  
  150. fail:
  151.     if (Iot.tr_node.io_Device) {
  152.     AbortIO(&Iot);
  153.     WaitIO(&Iot);
  154.     CloseDevice(&Iot);
  155.     }
  156.     DeletePort(TimPort);
  157.     if (Win)
  158.     CloseWindow(Win);
  159.     if (chan)
  160.     DClose(chan);
  161.     if (IntuitionBase)
  162.     CloseLibrary(IntuitionBase);
  163.     if (GfxBase)
  164.     CloseLibrary(GfxBase);
  165. }
  166.  
  167. #define LSIZE    (sizeof(LBuf)/sizeof(LBuf[0]))
  168. #define LIDX(i) ((Li + i) & (LSIZE-1))
  169.  
  170. uword LBuf[1024];    /*  The last N readings, (loadav * 256) + 1 */
  171.              /*  size must be power of 2 */
  172. uword Li;
  173. short Wbl, Wbt, Wbr, Wbb, Wh, Ww;
  174.  
  175. clearwindow()
  176. {
  177.     short i, j;
  178.     short height;
  179.  
  180.     Wbl = Win->BorderLeft;
  181.     Wbr = Win->BorderRight;
  182.     Wbt = Win->BorderTop;
  183.     Wbb = Win->BorderBottom;
  184.     Ww    = Win->Width;
  185.     Wh    = Win->Height;
  186.  
  187.     SetAPen(Rp, 0);
  188.     RectFill(Rp, Wbl, Wbt, Ww - Wbr, Wh - Wbb);
  189.  
  190.     Wbl += 2;
  191.     Wbr += 4;
  192.     SetAPen(Rp, 1);
  193.     for (i = 0, j = Ww - Wbr; i < LSIZE && j > Wbl; ++i, --j) {
  194.     height = (Wh - Wbb - Wbt) * LBuf[LIDX(i)] / (MaxLoad << 8);
  195.     if (Wh - Wbb - height > 0) {
  196.         Move(Rp, j, Wh - Wbb);
  197.         Draw(Rp, j, Wh - Wbb - height);
  198.  
  199.     }
  200.     }
  201.     SetAPen(Rp, 3);
  202.     for (i = 1; i < MaxLoad; ++i) {
  203.     height = Wh - Wbb - (Wh - Wbb - Wbt) * i / MaxLoad;
  204.     RectFill(Rp, Wbl - 2     , height, Wbl - 1     , height + 1);
  205.     RectFill(Rp, Ww - (Wbr - 3), height, Ww - (Wbr - 4), height + 1);
  206.     }
  207.     SetAPen(Rp, 1);
  208. }
  209.  
  210. updatewindow(i1, f1, i5, f5, i10, f10)
  211. {
  212.     short height;
  213.     short res = 0;
  214.  
  215.     while (i1 < 1000 && i1 >= MaxLoad) {
  216.     MaxLoad += LOADINCR;
  217.     res = 1;
  218.     }
  219.     if (res)
  220.     clearwindow();
  221.     sprintf(Title, "LoadAv %2ld.%02ld %2ld.%02ld %2ld.%02ld   Max=%2ld",
  222.     i1, f1, i5, f5, i10, f10, MaxLoad
  223.     );
  224.     SetWindowTitles(Win, Title, -1);
  225.     f1 = (f1  * 256) / 100;
  226.     f5 = (f5  * 256) / 100;
  227.     f10= (f10 * 256) / 100;
  228.     ScrollRaster(Rp, 1, 0, Wbl, Wbt, Ww - Wbr, Wh - Wbb);
  229.     Li = (Li-1) & (LSIZE-1);
  230.     LBuf[Li] = ((i1 << 8) | f1) + 1;
  231.  
  232.     height = (Wh - Wbb - Wbt) * LBuf[Li] / (MaxLoad << 8);
  233.     if (Wh - Wbb - height > 0) {
  234.     Move(Rp, Ww - Wbr, Wh - Wbb);
  235.     Draw(Rp, Ww - Wbr, Wh - Wbb - height);
  236.     }
  237.     SetAPen(Rp, 2);
  238.     Move(Rp, Wbl, Wbt + Rp->TxBaseline);
  239.     Text(Rp, Title + 7, 5);
  240.     SetAPen(Rp, 1);
  241. }
  242.  
  243.